#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
int w[N][N];
bool vis[N];

void dfs(int u) {
	vis[u] = 1;
	for (int v = 1; v <= n; v++) {
		if (w[u][v]) {
			cout << u << " " << v << " " << w[u][v] << endl;
			if (vis[v]) continue;
			dfs(v);
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int a, b, c; 
		cin >> a >> b >> c;
		w[a][b] = c;
		//w[b][a] = c;
	}
	dfs(1);
	return 0;
}
